"""
find() find_all() select()
1.查找标签
2.查找标签的文本内容
3.查找某标签的属性值
4.查找某标签内的标签
"""
import requests

from bs4 import BeautifulSoup

url = 'https://www.lgfdcw.com/cs/'


headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}

response = requests.get(url, headers=headers)
html = response.content.decode('gbk')
soup = BeautifulSoup(response.content.decode('gbk'),'lxml')
r1 = soup.find_all('tr',{'bgcolor':"#FFFFFF"})
#将解析出的内容保存在列表中
list_data = []
for e in r1:
    # print(e)
    name = e.select('a')[1].string
    # print(name)
    url = e.find('a')['href']
    # print(url)
    data={'标题':name,'详情链接':url}
    list_data.append(data)
#将列表写到文件
print(list_data)
with open('longuang.txt','w',encoding='utf-8') as f:
    f.write(list_data.__str__())